<template>
  <!--
    	作者：luoyiming
    	时间：2019-10-26
    	描述：商品管理-商品编辑-基础信息
    -->
  <div class="basic-setting-content pl16 pr16">
    <!--基本信息-->
    <div class="common-form">基本信息</div>
    <el-form-item
      label="商品名称："
      :rules="[{ required: true, message: '请填写商品名称' }]"
      prop="model.product_name"
    >
      <el-input v-model="form.model.product_name" class="max-w460"></el-input>
    </el-form-item>
    <el-form-item
      label="品牌："
      :rules="[{ required: true, message: '请填写品牌名称' }]"
      prop="model.brand_name"
    >
      <el-input v-model="form.model.brand_name" class="max-w460"></el-input>
    </el-form-item>
    <el-form-item
      label="产国/产地："
      :rules="[{ required: true, message: '请填写产国/产地' }]"
      prop="model.yieldly"
    >
      <el-input v-model="form.model.yieldly" class="max-w460"></el-input>
    </el-form-item>
    <el-form-item
      label="酒精度数："
      :rules="[{ required: true, message: '请填写酒精度数' }]"
      prop="model.alcoholic_strength"
    >
      <el-input
        v-model="form.model.alcoholic_strength"
        class="max-w460"
      ></el-input>
    </el-form-item>
    <el-form-item
      label="净含量："
      :rules="[{ required: true, message: '请填写净含量' }]"
      prop="model.net_content"
    >
      <el-input v-model="form.model.net_content" class="max-w460"></el-input>
    </el-form-item>
    <el-form-item
      label="所属分类："
      :rules="[{ required: true, message: '你选择商品分类' }]"
      prop="model.category_id"
    >
      <el-select v-model="form.model.category_id" clearable>
        <template v-for="cat in form.category">
          <el-option
            :value="cat.category_id"
            :key="cat.category_id"
            :label="cat.name"
          ></el-option>
          <template v-if="cat.child !== undefined" v-for="two in cat.child">
            <el-option
              :value="two.category_id"
              :key="two.category_id"
              :label="two.name"
              style="padding-left: 30px;"
            ></el-option>
            <template v-if="two.child !== undefined" v-for="three in two.child">
              <el-option
                :value="three.category_id"
                :key="three.category_id"
                :label="three.name"
                style="padding-left: 60px;"
              ></el-option>
            </template>
          </template>
        </template>
      </el-select>
    </el-form-item>
    <el-form-item label="商品品牌：">
      <el-select v-model="form.model.brandId" clearable>
        <template v-for="brand in form.brand">
          <el-option
            :value="brand.id"
            :key="brand.id"
            :label="brand.name"
          ></el-option>
        </template>
      </el-select>
    </el-form-item>
    <el-form-item label="已选品牌：">
      <template v-for="(item, key) in form.selectBrand">
        {{ item.name }}
        <el-button @click="delBrandClick(item.id, key)" type="text" size="small"
          >删除</el-button
        >
      </template>
    </el-form-item>
    <!--    <el-form-item label="预告商品：">-->
    <!--      <el-radio-group v-model="form.model.is_preview">-->
    <!--        <el-radio :label="1">开启</el-radio>-->
    <!--        <el-radio :label="0">关闭</el-radio>-->
    <!--      </el-radio-group>-->
    <!--    </el-form-item>-->
    <!--    <el-form-item v-if="form.model.is_preview==1" label="预告开启购买时间" :rules="[{ required: true, message: '请选择开启购买时间' }]" prop="model.preview_time">-->
    <!--      <el-date-picker-->
    <!--        v-model="form.model.preview_time"-->
    <!--        type="datetime"-->
    <!--        value-format="yyyy-MM-dd HH:mm:ss"-->
    <!--        placeholder="选择日期">-->
    <!--      </el-date-picker>-->
    <!--    </el-form-item>-->
    <el-form-item
      label="商品图片："
      :rules="[{ required: true, message: '请上传商品图片' }]"
      prop="model.image"
    >
      <div class="draggable-list">
        <draggable class="wrapper" v-model="form.model.image">
          <transition-group>
            <div
              class="item"
              v-for="(item, index) in form.model.image"
              :key="item.file_path"
            >
              <img v-img-url="item.file_path" />
              <a
                href="javascript:void(0);"
                class="delete-btn"
                @click.stop="deleteImg(index)"
                ><i class="el-icon-close"></i
              ></a>
            </div>
          </transition-group>
        </draggable>
        <div
          class="item img-select"
          @click="openProductUpload('image', 'image')"
        >
          <i class="el-icon-plus"></i>
        </div>
      </div>
    </el-form-item>
    <!--    <el-form-item label="商品视频：">-->
    <!--      <el-row>-->
    <!--        <div class="draggable-list">-->
    <!--          <div class="item img-select" v-if="form.model.video_id==0" @click="openProductUpload('video', 'video')"><i class="el-icon-plus"></i></div>-->
    <!--          <div v-if="form.model.video_id!=0">-->
    <!--            <video width="150" height="150" :src="form.model.video.file_path" :autoplay="false" controls>-->
    <!--              您的浏览器不支持 video 标签-->
    <!--            </video>-->
    <!--            <div>-->
    <!--              <el-button icon="el-icon-picture-outline" @click="delVideo">删除视频</el-button>-->
    <!--            </div>-->
    <!--          </div>-->
    <!--        </div>-->
    <!--      </el-row>-->
    <!--    </el-form-item>-->
    <!--    <el-form-item label="视频封面：">-->
    <!--      <el-row>-->
    <!--        <div class="draggable-list">-->
    <!--          <div class="item img-select" v-if="form.model.poster_id==0" @click="openProductUpload('image', 'poster')"><i class="el-icon-plus"></i></div>-->
    <!--          <div v-if="form.model.poster_id!=0" class="item" @click="openProductUpload('image', 'poster')">-->
    <!--            <img :src="form.model.poster.file_path" width="100" height="100" />-->
    <!--          </div>-->
    <!--        </div>-->
    <!--      </el-row>-->
    <!--    </el-form-item>-->
    <el-form-item label="商品卖点：">
      <el-input
        type="textarea"
        v-model="form.model.selling_point"
        class="max-w460"
      ></el-input>
    </el-form-item>
    <el-form-item label="是否推荐：">
      <el-radio-group v-model="form.model.recommend">
        <el-radio :label="1">是</el-radio>
        <el-radio :label="0">否</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="商品标签：">
      <el-select v-model="form.model.label_id" clearable>
        <el-option value="0" key="0" label="无"></el-option>
        <template v-for="bd in form.labels">
          <el-option :value="bd.id" :key="bd.id" :label="bd.name"></el-option>
        </template>
      </el-select>
    </el-form-item>
    <el-form-item label="商品标记：">
      <el-select v-model="form.model.mark_id" clearable>
        <el-option value="0" key="0" label="无"></el-option>
        <template v-for="mk in form.marks">
          <el-option :value="mk.id" :key="mk.id" :label="mk.name"></el-option>
        </template>
      </el-select>
    </el-form-item>
    <el-form-item label="是否显示：">
      <el-radio-group v-model="form.model.is_show">
        <el-radio :label="1">显示</el-radio>
        <el-radio :label="0">不显示</el-radio>
      </el-radio-group>
    </el-form-item>

    <el-form-item label="分享标题：" prop="model.share_title">
      <el-input v-model="form.model.share_title" class="max-w460"></el-input>
    </el-form-item>

    <el-form-item label="分享图片：">
      <el-row>
        <el-button
          icon="el-icon-picture-outline"
          @click="openProductUpload('share', 'share')"
          >选择图片</el-button
        >
        <div v-if="form.model.share_img_id != 0" class="img mt10">
          <img :src="form.model.share_img" width="100" height="100" />
        </div>
      </el-row>
    </el-form-item>

    <el-form-item label="赠送优惠券：">
      <el-input
        type="text"
        v-model="coupon_name"
        placeholder="请选择优惠券"
        class="max-w460"
        :disabled="true"
      ></el-input>
      <el-button @click="openConpon" icon="el-icon-plus">选择优惠券</el-button>
    </el-form-item>
    <el-form-item label="赠送数量：">
      <el-input
        v-model="form.model.give_coupon_num"
        type="number"
        class="max-w460"
      ></el-input>
    </el-form-item>

    <!--商品图片组件-->
    <Upload
      v-if="isProductUpload"
      :config="config"
      :isupload="isProductUpload"
      @returnImgs="returnProductImgsFunc"
    >
      上传图片
    </Upload>
    <!--选择优惠券-->
    <GetCoupon
      v-if="open_coupon"
      :open_add="open_coupon"
      @closeDialog="closeCouponFunc($event, 'add')"
    ></GetCoupon>
  </div>
</template>

<script>
import Upload from "@/components/file/Upload";
import draggable from "vuedraggable";
import ProductRelationBrandApi from "@/api/productRelationBrand";
import GetCoupon from "@/components/coupon/GetCoupon.vue";
import CouponApi from "@/api/coupon";

export default {
  components: {
    Upload,
    draggable,
    GetCoupon
  },
  data() {
    return {
      isProductUpload: false,
      config: {},
      file_name: "image",
      open_coupon: false,
      coupon_name: ""
    };
  },
  inject: ["form"],
  created() {
    let self = this;
    if (this.form.model.give_coupon_id) {
      CouponApi.couponDetail({
        coupon_id: this.form.model.give_coupon_id
      }).then(res => {
        self.coupon_name = res.data.detail.name;
      });
    }
  },
  methods: {
    /*打开上传图片*/
    openProductUpload: function(file_type, file_name) {
      this.file_name = file_name;
      if (file_type == "image") {
        this.config = {
          total: 9,
          file_type: "image"
        };
      } else if (file_type == "video") {
        this.config = {
          total: 1,
          file_type: "video"
        };
      } else if (file_type == "share") {
        this.config = {
          total: 1,
          file_type: "image"
        };
      }
      this.isProductUpload = true;
    },

    /*上传商品图片*/
    returnProductImgsFunc(e) {
      if (e != null) {
        if (this.file_name == "video") {
          this.form.model.video_id = e[0].file_id;
          this.form.model.video = e[0];
        } else if (this.file_name == "image") {
          let imgs = this.form.model.image.concat(e);
          this.$set(this.form.model, "image", imgs);
        } else if (this.file_name == "poster") {
          this.form.model.poster_id = e[0].file_id;
          this.form.model.poster = e[0];
        } else if (this.file_name == "share") {
          this.form.model.share_img_id = e[0].file_id;
          this.form.model.share_img = e[0].file_path;
        }
      }
      this.isProductUpload = false;
    },

    /*删除商品图片*/
    deleteImg(index) {
      this.form.model.image.splice(index, 1);
    },
    delVideo() {
      this.form.model.video_id = 0;
      this.form.model.video = {};
    },
    delBrandClick(id, index) {
      let self = this;
      ProductRelationBrandApi.delete({
        id: id
      }).then(data => {
        self.form.selectBrand.splice(index, 1);
        self.$message({
          message: "删除成功",
          type: "success"
        });
      });
    },
    /*选择优惠券*/
    openConpon() {
      this.open_coupon = true;
    },
    /*关闭优惠券*/
    closeCouponFunc(e) {
      this.open_coupon = false;
      console.log(e.params);
      if (e.type != "error") {
        this.form.model.give_coupon_id = e.params.coupon_id;
        this.coupon_name = e.params.name;
      }
    }
  }
};
</script>

<style>
.edit_container {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  line-height: 20px;
  color: #2c3e50;
}

.ql-editor {
  height: 400px;
}

.draggable-list {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}

.draggable-list .wrapper > span {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}

.draggable-list .item {
  position: relative;
  width: 110px;
  height: 110px;
  margin-top: 10px;
  margin-right: 10px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid #dddddd;
}

.draggable-list .delete-btn {
  position: absolute;
  top: 0;
  right: 0;
  width: 16px;
  height: 16px;
  background: red;
  line-height: 16px;
  font-size: 16px;
  color: #ffffff;
  display: none;
}

.draggable-list .item:hover .delete-btn {
  display: block;
}

.draggable-list .item img {
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  max-height: 100%;
  max-width: 100%;
}

.draggable-list .img-select {
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px dashed #dddddd;
  font-size: 30px;
}

.draggable-list .img-select i {
  color: #409eff;
}
</style>
